package com.bw.module_main.ui.guide

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_image.GlideApp
import com.bw.module_main.ui.MainViewModel
import com.bw.module_main.R
import com.bw.module_main.databinding.ActivityGuideBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = Router.Ui.GuideActivity)
@AndroidEntryPoint
class GuideActivity : BaseActivity<MainViewModel, ActivityGuideBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_guide

    override fun initData() {
        mBinding.vp.adapter = GuidePageAdapter()
        mBinding.vp.offscreenPageLimit = 1
        mBinding.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                when(position){
                    0 -> {
                        mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_checked
                        )
                        mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_uncheck
                        )
                        mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_uncheck
                        )
                    }
                    1 -> {
                        mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_uncheck
                        )
                        mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_checked
                        )
                        mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_uncheck
                        )
                    }
                    2 -> {
                        mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_uncheck
                        )
                        mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_uncheck
                        )
                        mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity,
                            R.drawable.bg_indicator_checked
                        )
                    }
                }

                if(position == 2){
                    show()
                }else{
                    hide()
                }
            }
        })

        mBinding.btn.setOnClickListener(){
            ARouter.getInstance().build(Router.Ui.MainActivity).navigation()
        }
    }


    private fun show(){
        mBinding.btn.visibility = View.VISIBLE
    }

    private fun hide(){
        mBinding.btn.visibility = View.GONE
    }


    private inner class GuidePageAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>(){

        private val guidePicRes = listOf(R.mipmap.pou3, R.mipmap.pou3, R.mipmap.pou3)
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            //创建 ImageView
            //使用 ImageView(parent.context) 构造一个新的 ImageView 实例。
            //设置 scaleType = CENTER_CROP：
            //图片会按照比例缩放，保证图片完全覆盖控件，超出部分会被裁剪。
            //设置布局参数为 MATCH_PARENT 宽高：
            //确保 ImageView 填充整个页面容器。
            //返回 RecyclerView.ViewHolder
            //使用匿名内部类继承 RecyclerView.ViewHolder 并传入构造好的 imageView。
            //这样每个页面项（Item）都会被封装成一个 ViewHolder，供 RecyclerView 复用。
            return object : RecyclerView.ViewHolder(ImageView(parent.context).apply {
                scaleType = ImageView.ScaleType.CENTER_CROP
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            }) {}
        }

        override fun getItemCount(): Int {

           return guidePicRes.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
           val imageView:ImageView = holder.itemView as ImageView
            GlideApp.with(imageView.context)
                .load(guidePicRes[position])
                .into(imageView)
        }

    }
}

